import 'package:flutter/material.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/blind_box_order_controller/blind_box_order_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:xinxuan/views/my/blindBox_order/blind_box_order_btns.dart';
import 'package:xinxuan/vo/blindbox/blindbox_order_list_vo.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:common_utils/common_utils.dart';

class BlindBoxOrderArr extends StatefulWidget {

  final BlindBoxOrderListVo seckillOrderListVo;
  final BlindBoxOrderPageController blindBoxOrderPageController;

  BlindBoxOrderArr({
    required this.seckillOrderListVo,
    required this.blindBoxOrderPageController
  });

  @override
  State<BlindBoxOrderArr> createState() => _BlindBoxOrderArrState();
}

class _BlindBoxOrderArrState extends State<BlindBoxOrderArr> with AutomaticKeepAliveClientMixin {

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (widget.seckillOrderListVo.data!.length <= 0) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              'assets/icons/icon_blindbox_order_list_def.png',
              width: 273.w,
              height: 146.h,
            ),
            SizedBox(height: 10.h),
            Text(
              '还没订单~去下一单！',
              style: TextStyle(
                color: textSubtitleColor,
                fontSize: 14.sp
              ),
            )
          ],
        ),
      );
    }
    return RefreshUtil.easyRefresh(
      onRefresh: () async {
        widget.blindBoxOrderPageController.getBlindBoxTabItemOrderList(widget.blindBoxOrderPageController.tabIndex.value);
      },
      onLoad: () async {
        widget.blindBoxOrderPageController.onloadBlindBoxOrderList();
      },
      slivers: [
        SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            BlindBoxOrderItem item = widget.seckillOrderListVo.data![index];
            return Container(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              margin: EdgeInsets.only(bottom: 12.h),
              child: InkWellUtil.shakeProofInkWell(Container(
                decoration: BoxDecoration(
                  color: bgWhiteColor,
                  borderRadius: BorderRadius.circular(5.w)
                ),
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 8.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              width: .5,
                              color: lineColor
                            )
                          )
                        ),
                        child: Row(
                          children: [
                            Expanded(child: Text(
                              '订单号：${item.orderSn}',
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: TextStyle(
                                color: textBlackColor,
                                fontWeight: FontWeight.bold,
                                fontSize: 14.sp
                              ),
                            )),
                            Text(
                              '${item.statusStr}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 14.sp
                              ),
                            )
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              width: .5,
                              color: lineColor
                            )
                          )
                        ),
                        child: Column(
                          children: [
                            Container(
                              // margin: EdgeInsets.only(bottom: 12.w),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  NetworkImageUtil(
                                    url: item.lottery!.product!.image ?? '', 
                                    width: 76.w, 
                                    height: 76.w,
                                    borderRadius: 5.w,
                                  ),
                                  SizedBox(width: 11.w),
                                  Expanded(child: Container(
                                    constraints: BoxConstraints(
                                      minHeight: 76.w
                                    ),
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          children: [
                                            Expanded(child: Text(
                                              '${item.lottery!.product!.title}',
                                              overflow: TextOverflow.ellipsis,
                                              maxLines: 2,
                                              style: TextStyle(
                                                fontSize: 14.sp,
                                                color: textBlackColor
                                              ),
                                            )),
                                            Text(
                                              'x1',
                                              style: TextStyle(
                                                color: textSubtitleColor,
                                                fontWeight: FontWeight.bold,
                                                fontSize: 12.sp
                                              ),
                                            )
                                          ],
                                        ),
                                        SizedBox(height: 12.w),
                                        Text(
                                          '${item.createdAt}',
                                          style: TextStyle(
                                            color: textSubtitleColor,
                                            fontSize: 12.sp
                                          ),
                                        )
                                      ],
                                    ),
                                  ))
                                ],
                              ),
                            ),
                            // Container(
                            //   alignment: Alignment.centerRight,
                            //   child: Text.rich(
                            //     TextSpan(
                            //       text: '合计支付：',
                            //       style: TextStyle(
                            //         color: textBlackColor,
                            //         fontSize: 14.sp,
                            //         fontWeight: FontWeight.bold
                            //       ),
                            //       children: [
                            //         TextSpan(
                            //           text: '¥',
                            //           style: TextStyle(
                            //             color: textBlackColor,
                            //             fontSize: 12.sp,
                            //             fontWeight: FontWeight.bold
                            //           )
                            //         ),
                            //         TextSpan(
                            //           text: '${item.totalAmount}',
                            //           style: TextStyle(
                            //             color: textBlackColor,
                            //             fontSize: 16.sp,
                            //             fontWeight: FontWeight.bold
                            //           )
                            //         )
                            //       ]
                            //     )
                            //   ),
                            // )
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 12.h),
                        alignment: Alignment.centerRight,
                        child: item.status == 2 && TextUtil.isEmpty(item.voucherCode) ? BlindBoxOrderBtns(
                          voucherCode: item.voucherCode ?? '', 
                          status: item.status!,
                          conversionPickingFun: () {
                            List<Map<String, dynamic>> mapArr = [{
                                'image': item.lottery!.product!.image,
                                'title': item.lottery!.product!.title,
                                'price': item.totalAmount,
                                'quantity': 1
                            }];
                            Get.dialog(DelDialog(
                              title: '转提货',
                              message: '转提货后，商品由接受人进行提货，本人不可对订单进行操作，确认吗？', 
                              onTap: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_ORDER_PAGE}${AppRouters.CONVERSION_PICKING_PAGE}',
                                  arguments: {
                                    'orderId': item.id,
                                    'products': mapArr,
                                    'status': item.status!,
                                    'statusStr': item.statusStr,
                                    'isOrderPage': true
                                  }
                                );
                              }
                            ));
                          },
                          pickUpCodeFun: () {
                            List<Map<String, dynamic>> mapArr = [{
                                'image': item.lottery!.product!.image,
                                'title': item.lottery!.product!.title,
                                'price': item.totalAmount,
                                'quantity': 1
                            }];
                            Get.dialog(DelDialog(
                              title: '获取提货券码',
                              message: '获取提货券码后，不可以再转给别人提货了，确认要生成提货券码吗？', 
                              onTap: () {
                                Get.toNamed(
                                  '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_ORDER_PAGE}${AppRouters.PICKUP_CODE_PAGE}',
                                  arguments: {
                                    'orderId': item.id,
                                    'products': mapArr,
                                    'status': item.status!,
                                    'statusStr': item.statusStr ?? '',
                                    'isOrderPage': true
                                  }
                                );
                              }
                            ));
                          },
                          seePickUpCodeFun: () {
                            Get.toNamed(
                              '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_ORDER_PAGE}${AppRouters.BLINDBOX_ORDER_DETAIL_PAGE}',
                              arguments: {
                                'id': item.id!,
                                'title': item.statusStr
                              }
                            );
                          }, 
                          isWriteoffOfficer: commonPageController.userData.value.isWriteoffOfficer ?? 0,
                          type: item.type ?? 0,
                          checkoff: () {
                            widget.blindBoxOrderPageController.checkoff(item.id!);
                          }, 
                        ) : Container(
                          width: 90.w,
                          margin: EdgeInsets.only(left: 12.w),
                          child: ButtonUtil(
                            text: '查看订单', 
                            fontSize: 14.w, 
                            borderRadius: 18.w, 
                            height: 36.h, 
                            onTop: () {
                              Get.toNamed(
                                '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_ORDER_PAGE}${AppRouters.BLINDBOX_ORDER_DETAIL_PAGE}',
                                arguments: {
                                  'id': item.id!,
                                  'title': item.statusStr
                                }
                              );
                            }
                          ),
                        )
                      )
                    ],
                  ),
                ),
              ), () {
                Get.toNamed(
                  '${AppRouters.COMMON_PAGE}${AppRouters.BLINDBOX_ORDER_PAGE}${AppRouters.BLINDBOX_ORDER_DETAIL_PAGE}',
                  arguments: {
                    'id': item.id!,
                    'title': item.statusStr
                  }
                );
              }),
            );
          }, childCount: widget.seckillOrderListVo.data!.length),
        ),
      ]
    );
  }

  @override
  bool get wantKeepAlive => true;
}